package com.ajie.slimming.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import net.simonvt.numberpicker.NumberPicker;
import net.simonvt.numberpicker.NumberPicker.OnValueChangeListener;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.ajie.slimming.R;
import com.ajie.slimming.bean.UserInfoItem;
import com.ajie.slimming.ui.BaseFragment;
import com.ajie.slimming.ui.UserInfoHelper;
import com.ajie.slimming.ui.adapter.UserInfoListAdapter;

/**
 * 个人中心
 * 
 * @author Jim
 */
public class UserInfoFragment extends BaseFragment implements
        OnItemClickListener, OnClickListener
{
    private ListView list = null;
    
    private List<UserInfoItem> infoList = new ArrayList<UserInfoItem>();
    
    private UserInfoListAdapter listAdapter = null;
    
    private ViewPager viewPager = null;
    
    private NumberPicker heightPicker, weightPicker, agePicker = null;
    
    private RadioGroup sexPicker = null;
    
    private List<View> viewList = new ArrayList<View>();
    
    private boolean isEditing = false;
    
    private TextView look_value_front, look_value_ending, target_value_front,
            target_value_ending;
    
    private UserInfoHelper helper;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        helper = UserInfoHelper.getInstance();
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_user_info,
                container,
                false);
        setTitle(R.string.user_info);
        //初始化信息列表
        initInfoListView(view);
        //初始化ViewPager
        initViewPager(inflater, container, view);
        //设置ViewPager
        setViewPager();
        //初始化ViewPager数据
        setViewPagerData();
        //初始化标题
        setTitle();
        return view;
    }
    
    private void setTitle()
    {
        if (helper.isInfoComplete())
        {
            //信息全部填了
            setOption(R.string.modify);
            refreshAnalysisViewData();
        }
        else
        {
            //如果信息缺失
            setOption(R.string.edit);
        }
        setOptionListener(this);
    }
    
    private void initInfoListView(View view)
    {
        list = (ListView) view.findViewById(R.id.user_info_list);
        list.setEnabled(false);
        listAdapter = new UserInfoListAdapter(getActivity());
        
        infoList.add(new UserInfoItem("性别", helper.getSex()));
        infoList.add(new UserInfoItem("身高", helper.getHeight()));
        infoList.add(new UserInfoItem("体重", helper.getWeight()));
        infoList.add(new UserInfoItem("年龄", helper.getAge()));
        listAdapter.setList(infoList);
        list.setAdapter(listAdapter);
        list.setOnItemClickListener(this);
    }
    
    private void initViewPager(LayoutInflater inflater, ViewGroup container,
            View view)
    {
        View analysisView = inflater.inflate(R.layout.fragment_user_info_analysis,
                container,
                false);
        View sexView = inflater.inflate(R.layout.fragment_user_info_sex,
                container,
                false);
        View ageView = inflater.inflate(R.layout.fragment_user_info_age,
                container,
                false);
        View heightView = inflater.inflate(R.layout.fragment_user_info_height,
                container,
                false);
        View weightView = inflater.inflate(R.layout.fragment_user_info_weight,
                container,
                false);
        
        viewList.add(sexView);
        viewList.add(heightView);
        viewList.add(weightView);
        viewList.add(ageView);
        viewList.add(analysisView);
        
        viewPager = (ViewPager) view.findViewById(R.id.input);
        viewPager.setAdapter(new MyAdapter(viewList));
        
        viewPager.setCurrentItem(5);
        
        sexPicker = (RadioGroup) sexView.findViewById(R.id.sex_group);
        agePicker = (NumberPicker) ageView.findViewById(R.id.age_chooser);
        heightPicker = (NumberPicker) heightView.findViewById(R.id.height_chooser);
        weightPicker = (NumberPicker) weightView.findViewById(R.id.weight_chooser);
        
        look_value_front = (TextView) analysisView.findViewById(R.id.look_value_front);
        look_value_ending = (TextView) analysisView.findViewById(R.id.look_value_ending);
        target_value_front = (TextView) analysisView.findViewById(R.id.target_value_front);
        target_value_ending = (TextView) analysisView.findViewById(R.id.target_value_ending);
    }
    
    private void setViewPagerData()
    {
        if (!TextUtils.isEmpty(helper.getSex()))
        {
            ((RadioButton) sexPicker.getChildAt(TextUtils.equals(helper.getSex(),
                    "男") ? 0 : 1)).setChecked(true);
        }
        if (!TextUtils.isEmpty(helper.getHeight()))
        {
            heightPicker.setValue(Integer.valueOf(helper.getHeight()));
        }
        if (!TextUtils.isEmpty(helper.getWeight()))
        {
            weightPicker.setValue(Integer.valueOf(helper.getWeight()));
        }
        if (!TextUtils.isEmpty(helper.getAge()))
        {
            agePicker.setValue(Integer.valueOf(helper.getAge()));
        }
    }
    
    private void setViewPager()
    {
        sexPicker.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                switch (checkedId)
                {
                    case R.id.sex_female:
                        infoList.get(0).setValue(getString(R.string.female));
                        break;
                    case R.id.sex_male:
                        infoList.get(0).setValue(getString(R.string.male));
                        break;
                    default:
                        break;
                }
                listAdapter.notifyDataSetChanged();
            }
        });
        heightPicker.setMinValue(100);
        heightPicker.setMaxValue(220);
        heightPicker.setOnValueChangedListener(new OnValueChangeListener()
        {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal,
                    int newVal)
            {
                infoList.get(1).setValue(newVal + "");
                listAdapter.notifyDataSetChanged();
            }
        });
        
        weightPicker.setMinValue(40);
        weightPicker.setMaxValue(120);
        weightPicker.setOnValueChangedListener(new OnValueChangeListener()
        {
            
            @Override
            public void onValueChange(NumberPicker picker, int oldVal,
                    int newVal)
            {
                infoList.get(2).setValue(newVal + "");
                listAdapter.notifyDataSetChanged();
            }
        });
        
        agePicker.setMinValue(10);
        agePicker.setMaxValue(100);
        agePicker.setOnValueChangedListener(new OnValueChangeListener()
        {
            
            @Override
            public void onValueChange(NumberPicker picker, int oldVal,
                    int newVal)
            {
                infoList.get(3).setValue(newVal + "");
                listAdapter.notifyDataSetChanged();
            }
        });
    }
    
    /** 刷新智能分析页面数据 */
    private void refreshAnalysisViewData()
    {
        look_value_front.setText(String.format("%.1f",
                helper.getStandardWeight()));
        look_value_ending.setText("~"
                + String.format("%.1f", helper.getStandardWeight() + 2.5)
                + "kg");
        target_value_front.setText(String.format("%.1f", helper.getTarget()));
        target_value_ending.setText("~"
                + String.format("%.1f", helper.getTarget() + 2.5) + "kg");
    }
    
    @Override
    public void onClick(View v)
    {
        if (isEditing)
        {
            if (!TextUtils.isEmpty(listAdapter.getValue(0))
                    && !TextUtils.isEmpty(listAdapter.getValue(1))
                    && !TextUtils.isEmpty(listAdapter.getValue(2))
                    && !TextUtils.isEmpty(listAdapter.getValue(3)))
            {
                boolean saveResult = helper.saveInfo(listAdapter.getValue(0),
                        listAdapter.getValue(1),
                        listAdapter.getValue(2),
                        listAdapter.getValue(3));
                if (saveResult)
                {
                    setOption(R.string.modify);
                    isEditing = false;
                    viewPager.setCurrentItem(5);
                    refreshAnalysisViewData();
                    showToast(R.string.save_success);
                }
                else
                {
                    showToast(R.string.save_failed);
                }
            }
            else
            {
                showToast(R.string.save_without_complate);
            }
        }
        else
        {
            isEditing = true;
            setOption(R.string.save);
        }
        list.setEnabled(isEditing);
    }
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id)
    {
        viewPager.setCurrentItem(position);
    }
    
    public class MyAdapter extends PagerAdapter
    {
        
        List<View> viewLists;
        
        public MyAdapter(List<View> lists)
        {
            viewLists = lists;
        }
        
        @Override
        public int getCount()
        { // 获得size
            return viewLists == null ? 0 : viewLists.size();
        }
        
        @Override
        public boolean isViewFromObject(View arg0, Object arg1)
        {
            return arg0 == arg1;
        }
        
        @Override
        public void destroyItem(View view, int position, Object object) // 销毁Item
        {
            ((ViewPager) view).removeView(viewLists.get(position));
        }
        
        @Override
        public Object instantiateItem(View view, int position) // 实例化Item
        {
            ((ViewPager) view).addView(viewLists.get(position));
            return viewLists.get(position);
        }
    }
}
